package com.spdg.ring.adapter;

import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.wolf.tools.StringUtil;

import com.spdg.ring.R;
import com.spdg.ring.sqlite.model.ArticleList;
import com.spdg.ring.util.ImageLoader;

/**
 * <p>Title: 九宫格适配器    </p>
 * <p>Description: 
 * 九宫格适配器，包括：
 * 1. 无标题
 * 2. 有标题
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-10-28             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class GridAdapter extends BaseAdapter {
	private List<ArticleList> mContentList = null;
	private Context mContext;
	private final LayoutInflater mInflater;

	public GridAdapter(Context ctx, List<ArticleList> list) {
		this.mContext = ctx;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (list == null) {
			mContentList = Collections.emptyList();
		}

		this.mContentList = list;
	}

	@Override
	public int getCount() {
		return mContentList.size();
	}

	@Override
	public Object getItem(int position) {
		return mContentList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.gridview_item, null);
		}

		ImageView mGridItemImg = (ImageView) convertView.findViewById(R.id.grid_item_img);
		TextView mGridItemTitle = (TextView) convertView.findViewById(R.id.grid_item_title);

		ArticleList entity = mContentList.get(position);

		String imgUrl = entity.getIcon();
		mGridItemImg.setImageResource(R.drawable.default_image);
		ImageLoader.getInstance().loadBitmapOvri(mContext, mGridItemImg, imgUrl,
				entity.getIconFile());

		String title = entity.getName();
		if (!StringUtil.isEmpty(title)) {
			mGridItemTitle.setText(Html.fromHtml(title));
			mGridItemTitle.setVisibility(View.VISIBLE);
		} else {
			mGridItemTitle.setVisibility(View.GONE);
		}

		return convertView;
	}

}
